/********************************************************************
	Rhapsody	: 7.5 
	Login		: kbe
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: LF_EventHandler
//!	Generated Date	: Thu, 27, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\LF_EventHandler.cpp
*********************************************************************/

//## auto_generated
#include "LF_EventHandler.h"
//## auto_generated
#include "Reactor.h"
//## operation LF_EventHandler(Reactor*,EventHandler*,ThreadPool*,Event_Type)
#include "ThreadPool.h"
//## package Default

//## class LF_EventHandler
LF_EventHandler::LF_EventHandler(Reactor* reactor, EventHandler* eh, ThreadPool* tp, const Event_Type& et) {
    itsThreadPool = NULL;
    itsEventHandler = NULL;
    //#[ operation LF_EventHandler(Reactor*,EventHandler*,ThreadPool*,Event_Type)
    itsThreadPool = tp; 
    itsEventHandler = eh;
    handle = eh->getHandle();
    itsReactor = reactor;
    reactor->registerHandler(*this, et);    
    
    //#]
}

LF_EventHandler::LF_EventHandler() {
    itsEventHandler = NULL;
    itsThreadPool = NULL;
}

LF_EventHandler::~LF_EventHandler() {
    //cleanUpRelations();
}

int LF_EventHandler::handleEvent(HANDLE h, Event_Type& et) {
    //#[ operation handleEvent(HANDLE,Event_Type)
    // Decorator pattern
	itsThreadPool->deactivateHandle(handle, et);
	itsThreadPool->promoteNewLeader(); // Promote new leader thread
	// Dispatch application-specific event processing code
	if (itsEventHandler->handleEvent(h, et) < 0)  // Processing
	{
		if (itsReactor) {
    		itsReactor->removeHandler(*this, type);// We are done - remove and delete it self 
    		//KBE!!! 
			delete itsEventHandler;
			//KBE!!! 
			delete this;
		}
	}
	else
		itsThreadPool->reactivateHandle(handle, et); // Reactivate
    return 0;
    //#]
}

EventHandler* LF_EventHandler::getItsEventHandler() const {
    return itsEventHandler;
}

ThreadPool* LF_EventHandler::getItsThreadPool() const {
    return itsThreadPool;
}

void LF_EventHandler::cleanUpRelations() {
    if(itsEventHandler != NULL)
        {
            itsEventHandler = NULL;
        }
    if(itsThreadPool != NULL)
        {
            itsThreadPool = NULL;
        }
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\LF_EventHandler.cpp
*********************************************************************/
